package com.sxm.yiwei.pinche;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.Order;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.utils.Calculator;
import com.sxm.yiwei.utils.HandleDataUtil;
import com.sxm.yiwei.views.AmountView;

import java.io.Serializable;
import java.util.Date;

public class PassengerTicketRefundActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_refund_style;
    private TextView tv_refund_desciption;
    private TextView tv_seats_bought;
    private TextView tv_total_price;
    private TextView tv_phone_and_style;
    private TextView tv_host_words;
    private TextView tv_start_pos;
    private TextView tv_end_pos;
    private TextView tv_order_id;
    private TextView tv_start_time;
    private TextView tv_compensation_money;
    private TextView tv_return_back;
    private TextView tv_pinche_rule;
    private TextView tv_pinche_count_rule;
    private Button btn_refund;
    private CheckBox checkBox;
    private AmountView amount_view;
    private EditText etAmount;
    private Order order;
    private int go_type;
    private int refund_seats=1;
    private double payment=0.0;
    private double payback=0.0;
    private ImageView iv_icon;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_passenger_ticket_refund);
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_refund_style = (TextView)findViewById(R.id.tv_refund_style);
        tv_refund_desciption = (TextView)findViewById(R.id.tv_refund_desciption);
        tv_seats_bought = (TextView)findViewById(R.id.tv_seats_bought);
        tv_total_price = (TextView)findViewById(R.id.tv_total_price);
        tv_phone_and_style = (TextView)findViewById(R.id.tv_phone_and_style);
        tv_host_words = (TextView)findViewById(R.id.tv_host_words);
        tv_start_pos = (TextView)findViewById(R.id.tv_start_pos);
        tv_end_pos = (TextView)findViewById(R.id.tv_end_pos);
        tv_order_id = (TextView)findViewById(R.id.tv_order_id);
        tv_start_time = (TextView)findViewById(R.id.tv_start_time);
        iv_icon = (ImageView)findViewById(R.id.iv_icon);
        tv_compensation_money = (TextView)findViewById(R.id.tv_compensation_money);
        tv_return_back = (TextView)findViewById(R.id.tv_return_back);
        checkBox = (CheckBox)findViewById(R.id.checkbox);
        tv_pinche_rule = (TextView)findViewById(R.id.tv_pinche_rule);
        tv_pinche_count_rule = (TextView)findViewById(R.id.tv_pinche_count_rule);
        btn_refund = (Button)findViewById(R.id.btn_refund);
        btn_refund.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
        btn_refund.setEnabled(false);
        amount_view = (AmountView)findViewById(R.id.amount_view);
        etAmount = (EditText)amount_view.findViewById(R.id.etAmount);
        etAmount.setEnabled(false);


    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_pinche_rule.setOnClickListener(this);
        tv_pinche_count_rule.setOnClickListener(this);
        btn_refund.setOnClickListener(this);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b){
                    btn_refund.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
                    btn_refund.setEnabled(false);
                }else {
                    btn_refund.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_enable));
                    btn_refund.setEnabled(true);
                }
            }
        });
        amount_view.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                refund_seats=amount;
                payment=Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),refund_seats*order.getSingle_money(),0);
                payback=Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),refund_seats*order.getSingle_money(),1);
                tv_compensation_money.setText(payment+"元");
                tv_return_back.setText(payback+"");
            }
        });
    }

    @Override
    public void initData() {
        order = (Order) getIntent().getSerializableExtra("order");
        go_type = getIntent().getIntExtra("go_type",-1);
        tv_title.setText("退票");
        payment=Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),order.getSingle_money(),0);
        payback=Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),order.getSingle_money(),1);
        if (go_type==Constant.LONG){
            tv_refund_style.setText("乘客—长途退票");
        }else if (go_type==Constant.CITY){
            tv_refund_style.setText("乘客—同城退票");

        }else if (go_type==Constant.TRAVEL){
            tv_refund_style.setText("乘客—旅游退票");

        }
        tv_seats_bought.setText(order.getSeats()+"个");
        tv_total_price.setText(order.getTotal_money()+"元");
        if (order.getVehicle_type()==Constant.BUS){
            tv_phone_and_style.setText(order.getPhone()+"[大巴]");
        }else {
            tv_phone_and_style.setText(order.getPhone()+"[私家车]");
        }
        if (order.getWho_created()==Constant.PASSENGER){
            tv_host_words.setText("车主留言："+order.getWords());
            tv_host_words.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.chezhu),null,null,null);
        }else {
            tv_host_words.setText("我的留言："+order.getWords());
            tv_host_words.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.chengke),null,null,null);
        }
        tv_start_pos.setText(order.getStart_place());
        tv_end_pos.setText(order.getEnd_place());
        tv_order_id.setText(order.getOrder_code());
        tv_start_time.setText(order.getDepart_time());
        tv_compensation_money.setText(payment+"元");
        tv_return_back.setText(payback+"");
        Picasso.with(this).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(iv_icon);

        amount_view.setGoods_storage(order.getSeats());
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_pinche_count_rule:
                Intent iPinCountRule = new Intent(this, RuleActivity.class);
                iPinCountRule.putExtra("type",Constant.PINCHE_COUNT_RULE);
                startActivity(iPinCountRule);
                break;
            case R.id.tv_pinche_rule:
                Intent iPinRule = new Intent(this, RuleActivity.class);
                iPinRule.putExtra("type",Constant.PINCHE_RULE);
                startActivity(iPinRule);
                break;
            case R.id.btn_refund:
                /*if (Integer.parseInt(etAmount.getText().toString())>Integer.parseInt(HandleDataUtil.withoutChinese(tv_seats_bought.getText().toString().trim()))){
                    showToast("您的退票数量超过已购票数，请重新选择");
                    return;
                }
                Intent intent = new Intent(this, BookPayActivity.class);
                intent.putExtra("orderCode","");
                intent.putExtra("amount", "");
                intent.putExtra("from", "");
                intent.putExtra("id","");
                intent.putExtra("alipay", "");
                intent.putExtra("yivipay","");
                startActivity(intent);*/
                JSONObject object = new JSONObject();
                object.put("role",Constant.PASSENGER+"");
                object.put("orderId",order.getOrder_id());
                object.put("total_amount",refund_seats*order.getSingle_money()+"");
                object.put("quitTicketSeats",refund_seats+"");
                object.put("payment",payment+"");
                object.put("quitAmount",payback+"");
                httpPost(Common.HOST+Common.REFUND_TICKET,object,Constant.REFUND_TICKET);
                break;
        }
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        Intent intent = new Intent(this, CompleteActivity.class);
        intent.putExtra("type",Constant.REFUND_OK);
        intent.putExtra("text_1","["+order.getStart_place()+"]到["+order.getEnd_place()+"]");
        intent.putExtra("text_2",order.getOrder_code());
        startActivity(intent);
    }
}
